package com.pdsu.xpf.graduation01.control;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.xpf.graduation01.domain.Commodity;
import com.pdsu.xpf.graduation01.domain.CommodityType;
import com.pdsu.xpf.graduation01.domain.Type;
import com.pdsu.xpf.graduation01.domain.User;
import com.pdsu.xpf.graduation01.dto.CommodityDTO;
import com.pdsu.xpf.graduation01.dto.CommodityLikeDTO;
import com.pdsu.xpf.graduation01.dto.PayCommodityDTO;
import com.pdsu.xpf.graduation01.service.impl.CommodityServiceImpl;
import com.pdsu.xpf.graduation01.service.impl.PayCommodityServiceImpl;
import com.pdsu.xpf.graduation01.service.impl.UserServiceImpl;
import com.pdsu.xpf.graduation01.utils.JsonFormat;
import com.pdsu.xpf.graduation01.utils.Result;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/commodity")
public class CommodityControl {

    @Autowired
    private ServletContext servletContext;
    @Autowired
    private ResourceLoader resourceLoader;



    final
    UserServiceImpl userService;


    final
    PayCommodityServiceImpl payCommodityService;
    final
    CommodityServiceImpl commodityService;

    public CommodityControl(CommodityServiceImpl commodityService, UserServiceImpl userService, PayCommodityServiceImpl payCommodityService) {
        this.commodityService = commodityService;
        this.userService = userService;
        this.payCommodityService = payCommodityService;
    }

    //findCommodityByKey

    @ResponseBody
    @RequestMapping("/findCommodityByKey")
    public Result findCommodityByKey(HttpServletRequest request,String key) throws JsonProcessingException {

        System.out.println(key);
        String key_t = "%"+key+"%";
        List<Commodity> commodities=commodityService.findCommodityByKey(key_t);

        String data_json = JsonFormat.ListToJson(commodities);
        System.out.println(commodities.size());
        return Result.success(data_json);
    }

    @ResponseBody
    @RequestMapping("/getLikeCommodity")
    public Result getLikeCommodity(HttpServletRequest request,int uid) throws JsonProcessingException {
        List<CommodityLikeDTO> commodityLikeDTOS=commodityService.getLikeCommodity(uid);
        String commodities_json=JsonFormat.ListToJson(commodityLikeDTOS);
        System.out.println(commodities_json);
        return Result.success(commodities_json);
    }

    @ResponseBody
    @RequestMapping("/addCommodityToCar")
    public Result addCommodityToCar(HttpServletRequest request,int uid,int cid) throws JsonProcessingException {
        System.out.println("uid"+uid);
        System.out.println("cid"+cid);
        Integer have=commodityService.carIsHaveCommodity(uid,cid);
        if (have==null){
           Integer i= commodityService.addCommodityToCar(uid,cid);
//            List<PayCommodityDTO> payCommodityDTOS=payCommodityService.findPayCommodityByUID(uid);
//            String json = JsonFormat.ListToJson(payCommodityDTOS);
            return Result.success();


        }else {
            return Result.failed();
        }

    }


    @ResponseBody
    @RequestMapping("/addLikeCommodity")
    public Result addLikeCommodity(HttpServletRequest request,int uid,int cid) throws JsonProcessingException {
        System.out.println("uid"+uid);
        System.out.println("cid"+cid);
        int i= commodityService.addLikeCommodity(uid,cid);
        return Result.success(i);
    }



    @ResponseBody
    @RequestMapping("/delLikeCommodity")
    public Result delLikeCommodity(HttpServletRequest request,int uid,int cid) throws JsonProcessingException {

        System.out.println("uid"+uid);
        System.out.println("cid"+cid);
        int i= commodityService.delLikeCommodity(uid,cid);
        return Result.success(i);
    }

//    commodityIsLike

    @ResponseBody
    @RequestMapping("/commodityIsLike")
    public Result commodityIsLike(HttpServletRequest request,int uid,int cid) throws JsonProcessingException {

        System.out.println("uid"+uid);
        System.out.println("cid"+cid);
        Integer i= commodityService.commodityIsLike(uid,cid);

        if (i==null){
            return Result.failed();
        }
            return Result.success();


    }

    @ResponseBody
    @RequestMapping("/getRecommend")
    public Result getRecommend(HttpServletRequest request) throws JsonProcessingException {
        List<Commodity> commodities= commodityService.findAllRecommendCommodity();

        String commodities_json=JsonFormat.ListToJson(commodities);
        System.out.println(commodities_json);
        return Result.success(commodities_json);
    }


    @ResponseBody
    @RequestMapping("/getCommodityByCID")
    public Result getCommodityByCID(HttpServletRequest request,int cid) throws JsonProcessingException {

        Commodity commodity=commodityService.getCommodityByCID(cid);
        String commodity_json=JsonFormat.ObjToJson(commodity);

        System.out.println(commodity_json);
        return Result.success(commodity_json);
    }

    @ResponseBody
    @RequestMapping("/getCommodityByCTID")
    public Result getCommodityByCTID(HttpServletRequest request,int ctid) throws JsonProcessingException {

        List<Commodity> commoditys=commodityService.getCommodityByCTID(ctid);
        String commodity_json=JsonFormat.ListToJson(commoditys);
        System.out.println(commodity_json);
        return Result.success(commodity_json);
    }
    @ResponseBody
    @RequestMapping("/getCommodityStoreByCID")
    public Result getCommodityStoreByCID(HttpServletRequest request,int cid) throws JsonProcessingException {

        System.out.println(cid+"cid--");

        //查询商品
        Commodity commodity=commodityService.getCommodityByCID(cid);


        //查询归属者
        User user= userService.findUserByUID(commodity.getUid());

        System.out.println(user.toString());
        System.out.println(user.toString());
        //赋值
        CommodityDTO commodityDTO=new CommodityDTO();
        commodityDTO.setCid(commodity.getCid());
        commodityDTO.setCmessage(commodity.getCmessage());
        commodityDTO.setCname(commodity.getCname());
        commodityDTO.setDate(commodity.getDate());
        commodityDTO.setImage(commodity.getImage());
        commodityDTO.setCnumber(commodity.getCnumber());
        commodityDTO.setUser_miaoshu(user.getMiaoshu());
        commodityDTO.setUser_sudu(user.getSudu());
        commodityDTO.setUser_taidu(user.getTaidu());
        commodityDTO.setPrice(commodity.getPrice());
        commodityDTO.setUsername(user.getUsername());

        String commodity_json=JsonFormat.ObjToJson(commodityDTO);

        System.out.println(commodity_json);
        return Result.success(commodity_json);
    }

    @ResponseBody
    @PostMapping("/uploadImage")
    public Result uploadImage(HttpServletRequest request ,@RequestParam("file") MultipartFile file) throws JsonProcessingException {

        String json_t = request.getParameter("commodity");
        Commodity commodity= JsonFormat.JsonToObj(json_t,Commodity.class);
        System.out.println("js"+json_t);
        System.out.println(file.getOriginalFilename());
        String fileName = file.getOriginalFilename();
        String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1);

        // 生成新的文件名
        String newFileName = UUID.randomUUID().toString() + "." + fileExt;
        try {
            // 构造文件保存路径
//            Resource resource = resourceLoader.getResource("classpath:static/upload/" + newFileName);
//
//            // 将上传的文件保存到指定路径
//            file.transferTo(resource.getFile());
//            String rootPath = servletContext.getContextPath();
//            String realPath = ResourceUtils.getURL("classpath:").getPath() + "static";
//            System.out.println(realPath);
//            System.out.println(request.getServletContext().getContextPath());
//            System.out.println(request.getServletContext().getRealPath("/"));
            // 构造文件保存路径
//            String savePath = realPath+"/" + newFileName; // 保存到 target 文件夹的 static/upload 目录下
            //保存到项目里
            // 将上传的文件保存到指定路径

            //保存到外部指定文件夹
            String savePath="D:/TTT/"+newFileName;
            file.transferTo(new File(savePath));
            String url_image= "http://10.0.2.2:8088/"+newFileName;
            commodity.setImage(url_image);
            commodityService.insertACommodity(commodity);
            // 返回上传成功提示
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            // 返回上传失败提示
            return Result.failed();
        }
    }

    @ResponseBody
    @RequestMapping("/getAllType")
    public Result getAllType(HttpServletRequest request) throws JsonProcessingException {


        List<Type> types = commodityService.getAllType();
          String types_json=JsonFormat.ListToJson(types);
        System.out.println(types_json);
        return Result.success(types_json);
    }

    @ResponseBody
    @RequestMapping("/getCommodityTypeByTID")
    public Result getCommodityTypeByTID(HttpServletRequest request,int tid) throws JsonProcessingException {

        List<CommodityType> commodityTypes= commodityService.getCommodityTypeByTID(tid);
        String commodityType_json = JsonFormat.ListToJson(commodityTypes);
        System.out.println(commodityType_json);
        return Result.success(commodityType_json);
    }





}
